<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>班级列表</title>
    <link rel="stylesheet" type="text/css" href="/static/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/static/easyui/css/demo.css">
    <script type="text/javascript" src="/static/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/static/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/easyui/js/validateExtends.js"></script>
    <script type="text/javascript">
        $(function () {
            //datagrid初始化
            var dataList = $('#dataList');
            dataList.datagrid({
                title: '班级列表',
                iconCls: 'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                url: "/clazz/list",
                idField: 'id',
                singleSelect: false,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                sortName: 'id',
                sortOrder: 'DESC',
                remoteSort: false,
                columns: [
                    [
                        {field: 'chk', checkbox: true, width: 50},
                        {field: 'id', title: 'ID', width: 50, sortable: true},
                        {field: 'name', title: '班级名称', width: 200},
                    ]
                ],
                toolbar: "#toolbar"
            });
            //设置分页控件
            var p = dataList.datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数，默认为10
                pageList: [10, 20, 30, 50, 100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
            //设置工具类按钮
            $("#add").click(function () {
                $("#addDialog").dialog("open");
            });
            //删除
            $("#delete").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                if (selectRows == null) {
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else {
                    var ids = [];
                    $(selectRows).each(function (i, row) {
                        ids[i] = row.id;
                    });
                    $.messager.confirm("消息提醒", "将删除与班级相关的所有数据(包括学生)，确认继续？", function (r) {
                        if (r) {
                            $.ajax({
                                type: "post",
                                url: "/clazz/delete",
                                data: {ids: ids},
                                success: function (msg) {
                                    if (msg === "success") {
                                        $.messager.alert("消息提醒", "删除成功!", "info");
                                        //刷新表格
                                        $("#dataList").datagrid("reload");
                                    } else {
                                        $.messager.alert("消息提醒", "删除失败!", "warning");
                                        return;
                                    }
                                }
                            });
                        }
                    });
                }
            });


            //设置添加班级窗口
            $("#addDialog").dialog({
                title: "添加班级",
                width: 500,
                height: 400,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text: '添加',
                        plain: true,
                        iconCls: 'icon-add',
                        handler: function () {
                            var validate = $("#addForm").form("validate");
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据!", "warning");
                                return;
                            } else {
                                $.ajax({
                                    type: "post",
                                    url: "/clazz/add",
                                    data: $("#addForm").serialize(),
                                    success: function (msg) {
                                        if (msg === "success") {
                                            $.messager.alert("消息提醒", "添加成功!", "info");
                                            //关闭窗口
                                            $("#addDialog").dialog("close");
                                            //清空原表格数据
                                            $("#add_name").textbox('setValue', "");

                                            //重新刷新页面数据
                                            dataList.datagrid("reload");

                                        } else {
                                            $.messager.alert("消息提醒", "添加失败!", "warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-reload',
                        handler: function () {
                            $("#add_name").textbox('setValue', "");
                        }
                    },
                ]
            });

        });
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">
</table>

<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton"
                                                     data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>

</div>

<!-- 添加窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8">
            <tr>
                <td>班级名称:</td>
                <!--validType="repeat_clazz['#add_gradeList']"-->
                <td>
                    <input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text"
                           name="name" data-options="required:true, missingMessage:'不能为空'"/>
                </td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>